package com.bidr.kernel.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Title: FileUtil
 * Description: Copyright: Copyright (c) 2022 Company: Sharp Ltd.
 *
 * @author Sharp
 * @since 2023/03/22 11:14
 */
public class FileUtil {


    public static InputStream getResourcesFileInputStream(String fileName) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName);
    }

    public static File createNewFile(String pathName) {
        File file = new File(getPath() + pathName);
        if (file.exists()) {
            file.delete();
        } else {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        return file;
    }

    public static String getPath() {
        return FileUtil.class.getResource("/").getPath();
    }

    public static File readFile(String pathName) {
        return new File(getPath() + pathName);
    }

    public static File readUserHomeFile(String pathName) {
        return new File(System.getProperty("user.home") + File.separator + pathName);
    }

    public static String readTxt(File file) throws IOException {
        StringBuilder prompt = new StringBuilder();
        LineIterator lineIterator = FileUtils.lineIterator(file, "UTF-8");
        while (lineIterator.hasNext()) {
            prompt.append(lineIterator.nextLine());
        }
        return prompt.toString();
    }

    public static String readTxt(InputStream is) {
        StringBuilder prompt = new StringBuilder();
        LineIterator lineIterator = IOUtils.lineIterator(is, "UTF-8");
        while (lineIterator.hasNext()) {
            prompt.append(lineIterator.nextLine());
        }
        return prompt.toString();
    }
}
